<?php
namespace BP\Sys;

use BP\En\EntityNoName;

require_once("BP/Difference/Handler/CommonUtils.php");
require_once("BP/Tools/Json.php");
require_once("BP/Tools/StringHelper.php");
class SFTable extends EntityNoName {
	public function GenerHisDataTable () 
	{
		$src = SFDBSrc::constructor__String($this->getFK_SFDBSrc());
		if (($this->getSrcType() == SrcType::$BPClass))
		{
			$ens = $ClassFactory->GetEns($this->getNo());
			return $ens->RetrieveAllToTable();
		}
		if (($this->getSrcType() == SrcType::$TableOrView))
		{
			$sql = (((("SELECT " . $this->getColumnValue()) . " No, ") . $this->getColumnText()) . " Name");
			if (($this->getCodeStruct() == CodeStruct::$Tree))
			{
				$sql += ((", " . $this->getParentValue()) . " ParentNo");
			}
			$sql += (" FROM " . $this->getSrcTable());
			return $src->RunSQLReturnTable($sql);
		}
		if (($this->getSrcType() == SrcType::$SQL))
		{
			$runObj = $this->getSelectStatement();
			if ($DataType->IsNullOrEmpty($runObj))
			{
				throw new \RuntimeException((((((("@外键类型SQL配置错误," . $this->getNo()) . " ") . $this->getName()) . " 是一�?(SQL)类型(") . $this->GetValStrByKey("SrcType")) . ")，但是没有配置sql."));
			}
			if (($runObj . NULL))
			{
				$runObj = "";
			}
			$runObj = $runObj->replace("~", "'");
			if ($runObj->contains("@WebUser.getNo()"))
			{
				$runObj = $runObj->replace("@WebUser.getNo()", $WebUser->getNo());
			}
			if ($runObj->contains("@WebUser.getName()"))
			{
				$runObj = $runObj->replace("@WebUser.getName()", $WebUser->getName());
			}
			if ($runObj->contains("@WebUser.getFK_Dept()"))
			{
				$runObj = $runObj->replace("@WebUser.getFK_Dept()", $WebUser->getFK_Dept());
			}
			$dt = $src->RunSQLReturnTable($runObj);
			return $dt;
		}
		if (($this->getSrcType() == SrcType::$CreateTable))
		{
			$sql = ("SELECT No, Name FROM " . $this->getNo());
			return $src->RunSQLReturnTable($sql);
		}
		return NULL;
	}
	public function GenerHisJson () 
	{
		return $BP->Tools->Json->ToJson($this->GenerHisDataTable());
	}
	public function GenerSFTableNewNo () 
	{
		$table = $this->getSrcTable();
		try 
		{
			$sql = NULL;
			$field = "No";
			switch ($this->getEnMap()->getEnDBUrl()->getDBType()) {
				case $MSSQL:
					$sql = ((("SELECT CONVERT(INT, MAX(CAST(" . $field) . " as int)) )+1 AS No FROM ") . $table);
					break;
				case $PostgreSQL:
					$sql = ((("SELECT to_number( MAX(" . $field) . ") ,'99999999')+1   FROM ") . $table);
					break;
				case $Oracle:
					$sql = ((("SELECT MAX(" . $field) . ") +1 AS No FROM ") . $table);
					break;
				case $MySQL:
					$sql = ((("SELECT CONVERT(MAX(CAST(" . $field) . " AS SIGNED INTEGER)),SIGNED) +1 AS No FROM ") . $table);
					break;
				case $Informix:
					$sql = ((("SELECT MAX(" . $field) . ") +1 AS No FROM ") . $table);
					break;
				case $Access:
					$sql = ((("SELECT MAX( [" . $field) . "]) +1 AS  No FROM ") . $table);
					break;
				default:
					throw new \RuntimeException("error");
			}
			$str = $String->valueOf(DBAccess->RunSQLReturnValInt($sql, 1));
			if (($str->equals("0") || $str->equals("")))
			{
				$str = "1";
			}
			return $StringHelper->padLeft($str, 3, '0');
		}
		catch (\RuntimeException $e)
		{
			return "";
		}
	}
	public function InvokeWebService ($url, $methodname, $args) // [String url, String methodname, Object[] args]
	{
		return NULL;
	}
	public function getFK_SFDBSrc () 
	{
		return $this->GetValStringByKey(SFTableAttr::$FK_SFDBSrc);
	}
	public function setFK_SFDBSrc ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$FK_SFDBSrc, $value);
	}
	public function getFK_SFDBSrcT () 
	{
		return $this->GetValRefTextByKey(SFTableAttr::$FK_SFDBSrc);
	}
	public function getRootVal () 
	{
		return $this->GetValStringByKey(SFTableAttr::$RootVal);
	}
	public function setRootVal ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$RootVal, $value);
	}
	public function getCashMinute () 
	{
		return $this->GetValIntByKey(SFTableAttr::$CashMinute);
	}
	public function setCashMinute ($value) // [int value]
	{
		$this->SetValByKey(SFTableAttr::$CashMinute, $value);
	}
	public function getSrcTable () 
	{
		$str = $this->GetValStringByKey(SFTableAttr::$SrcTable);
		if (($str->equals("") . ($str . NULL)))
		{
			return $this->getNo();
		}
		return $str;
	}
	public function setSrcTable ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$SrcTable, $value);
	}
	public function getColumnValue () 
	{
		return $this->GetValStringByKey(SFTableAttr::$ColumnValue);
	}
	public function setColumnValue ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$ColumnValue, $value);
	}
	public function getColumnText () 
	{
		return $this->GetValStringByKey(SFTableAttr::$ColumnText);
	}
	public function setColumnText ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$ColumnText, $value);
	}
	public function getParentValue () 
	{
		return $this->GetValStringByKey(SFTableAttr::$ParentValue);
	}
	public function setParentValue ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$ParentValue, $value);
	}
	public function getSelectStatement () 
	{
		return $this->GetValStringByKey(SFTableAttr::$SelectStatement);
	}
	public function setSelectStatement ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$SelectStatement, $value);
	}
	public function getRDT () 
	{
		return $this->GetValStringByKey(SFTableAttr::$RDT);
	}
	public function setRDT ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$RDT, $value);
	}
	public function getIsClass () 
	{
		if ($this->getNo()->contains("."))
		{
			return  TRUE ;
		}
		else
		{
			return  FALSE ;
		}
	}
	public function getIsTree () 
	{
		if (($this->getCodeStruct() == CodeStruct::$NoName))
		{
			return  FALSE ;
		}
		return  TRUE ;
	}
	public function getSrcType () 
	{
		if (($this->getNo()->contains("BP.") ==  TRUE ))
		{
			return SrcType::$BPClass;
		}
		else
		{
			$src = SrcType->forValue($this->GetValIntByKey(SFTableAttr::$SrcType));
			if (($src == SrcType::$BPClass))
			{
				return SrcType::$CreateTable;
			}
			return $src;
		}
	}
	public function setSrcType ($value) // [SrcType value]
	{
		$this->SetValByKey(SFTableAttr::$SrcType, $value->getValue());
	}
	public function getSrcTypeText () 
	{
		switch ($this->getSrcType()) {
			case $TableOrView:
				if ($this->getIsClass())
				{
					return "<img src='/WF/Img/Class.png' width='16px' broder='0' />实体�?";
				}
				else
				{
					return "<img src='/WF/Img/Table.gif' width='16px' broder='0' />�?/视图";
				}
			case $SQL:
				return "<img src='/WF/Img/SQL.png' width='16px' broder='0' />SQL表达�?";
			case $WebServices:
				return "<img src='/WF/Img/WebServices.gif' width='16px' broder='0' />WebServices";
			default:
				return "";
		}
	}
	public function getCodeStruct () 
	{
		return CodeStruct->forValue($this->GetValIntByKey(SFTableAttr::$CodeStruct));
	}
	public function setCodeStruct ($value) // [CodeStruct value]
	{
		$this->SetValByKey(SFTableAttr::$CodeStruct, $value->getValue());
	}
	public function getCodeStructT () 
	{
		return $this->GetValRefTextByKey(SFTableAttr::$CodeStruct);
	}
	public function getFK_Val () 
	{
		return $this->GetValStringByKey(SFTableAttr::$FK_Val);
	}
	public function setFK_Val ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$FK_Val, $value);
	}
	public function getTableDesc () 
	{
		return $this->GetValStringByKey(SFTableAttr::$TableDesc);
	}
	public function setTableDesc ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$TableDesc, $value);
	}
	public function getDefVal () 
	{
		return $this->GetValStringByKey(SFTableAttr::$DefVal);
	}
	public function setDefVal ($value) // [String value]
	{
		$this->SetValByKey(SFTableAttr::$DefVal, $value);
	}
	public function getHisEns () 
	{
		if ($this->getIsClass())
		{
			$ens = $BP->En->ClassFactory->GetEns($this->getNo());
			$ens->RetrieveAll();
			return $ens;
		}
		$ges = new GENoNames($this->getNo(), $this->getName());
		$ges->RetrieveAll();
		return $ges;
	}
	public function getHisUAC () 
	{
		$uac = new UAC();
		$uac->OpenForSysAdmin();
		$uac->IsInsert =  FALSE ;
		return $uac;
	}
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public static function constructor__String ($mypk) // [String mypk]
	{
		$me = new self();
		$me->setNo($mypk);
		try 
		{
			$me->Retrieve();
		}
		catch (\RuntimeException $ex)
		{
			switch ($me->getNo()) {
				case "BP.Pub.NYs":
					$me->setName("年月");
					$me->setFK_Val("FK_NY");
					$me->Insert();
					break;
				case "BP.Pub.YFs":
					$me->setName("�?");
					$me->setFK_Val("FK_YF");
					$me->Insert();
					break;
				case "BP.Pub.Days":
					$me->setName("�?");
					$me->setFK_Val("FK_Day");
					$me->Insert();
					break;
				case "BP.Pub.NDs":
					$me->setName("�?");
					$me->setFK_Val("FK_ND");
					$me->Insert();
					break;
				default:
					throw new \RuntimeException($ex->getMessage());
			}
		}
		return $me;
	}
	public function getEnMap () 
	{
		if (($this->get_enMap() != NULL))
		{
			return $this->get_enMap();
		}
		$map = new Map("Sys_SFTable", "字典�?");
		$map->Java_SetDepositaryOfEntity($Depositary->None);
		$map->Java_SetDepositaryOfMap($Depositary->Application);
		$map->Java_SetEnType($EnType->Sys);
		$map->AddTBStringPK(SFTableAttr::$No, NULL, "表英文名�?",  TRUE ,  FALSE , 1, 200, 20);
		$map->AddTBString(SFTableAttr::$Name, NULL, "表中文名�?",  TRUE ,  FALSE , 0, 200, 20);
		$map->AddDDLSysEnum(SFTableAttr::$SrcType, 0, "数据表类�?",  TRUE ,  FALSE , SFTableAttr::$SrcType, "@0=本地的类@1=创建表@2=表或视图@3=SQL查询表@4=WebServices@5=微服务Handler外部数据源@6=JavaScript外部数据源@7=动�?�Json");
		$map->AddDDLSysEnum(SFTableAttr::$CodeStruct, 0, "字典表类�?",  TRUE ,  FALSE , SFTableAttr::$CodeStruct);
		$map->AddTBString(SFTableAttr::$RootVal, NULL, "根节点�??",  FALSE ,  FALSE , 0, 200, 20);
		$map->AddTBString(SFTableAttr::$FK_Val, NULL, "默认创建的字段名",  TRUE ,  FALSE , 0, 200, 20);
		$map->AddTBString(SFTableAttr::$TableDesc, NULL, "表描�?",  TRUE ,  FALSE , 0, 200, 20);
		$map->AddTBString(SFTableAttr::$DefVal, NULL, "默认�?",  TRUE ,  FALSE , 0, 200, 20);
		$map->AddDDLEntities(SFTableAttr::$FK_SFDBSrc, "local", "数据�?", SFDBSrcs::constructor__(),  TRUE );
		$map->AddTBString(SFTableAttr::$SrcTable, NULL, "数据源表",  FALSE ,  FALSE , 0, 200, 20);
		$map->AddTBString(SFTableAttr::$ColumnValue, NULL, "显示的�??(编号�?)",  FALSE ,  FALSE , 0, 200, 20);
		$map->AddTBString(SFTableAttr::$ColumnText, NULL, "显示的文�?(名称�?)",  FALSE ,  FALSE , 0, 200, 20);
		$map->AddTBString(SFTableAttr::$ParentValue, NULL, "父级�?(父级�?)",  FALSE ,  FALSE , 0, 200, 20);
		$map->AddTBString(SFTableAttr::$SelectStatement, NULL, "查询语句",  TRUE ,  FALSE , 0, 1000, 600,  TRUE );
		$map->AddTBDateTime(SFTableAttr::$RDT, NULL, "加入日期",  FALSE ,  FALSE );
		$map->AddSearchAttr(SFTableAttr::$FK_SFDBSrc);
		$rm = new RefMethod();
		$rm->Title = "查看数据";
		$rm->ClassMethodName = ($this . ".DoEdit");
		$rm->refMethodType = $RefMethodType->RightFrameOpen;
		$rm->IsForEns =  FALSE ;
		$map->AddRefMethod($rm);
		$this->set_enMap($map);
		return $this->get_enMap();
	}
	public function DoMangDBSrc () 
	{
		return (SystemConfig::getCCFlowWebPath() . "WF/Comm/Sys/SFDBSrcNewGuide.htm");
	}
	public function DoGuide () 
	{
		return (SystemConfig::getCCFlowWebPath() . "WF/Admin/FoolFormDesigner/CreateSFGuide.htm");
	}
	public function DoEdit () 
	{
		if ($this->getIsClass())
		{
			return ((SystemConfig::getCCFlowWebPath() . "WF/Comm/Ens.htm?EnsName=") . $this->getNo());
		}
		else
		{
			return ((SystemConfig::getCCFlowWebPath() . "WF/Admin/FoolFormDesigner/SFTableEditData.htm?FK_SFTable=") . $this->getNo());
		}
	}
	public function IsCanDelete () 
	{
		$attrs = MapAttrs::constructor__();
		$attrs->Retrieve(MapAttrAttr::$UIBindKey, $this->getNo());
		if (($attrs->size() != 0))
		{
			$err = "";
			foreach ($attrs->ToJavaList() as $item) 			{
				$err += (((" @ " . $item->getMyPK()) . " ") . $item->getName());
			}
			return (("err@如下实体字段在引�?:" . $err) . "。您不能删除该表�?");
		}
		return NULL;
	}
	protected function beforeDelete () 
	{
		$delMsg = $this->IsCanDelete();
		if (($delMsg . NULL))
		{
			throw new \RuntimeException($delMsg);
		}
		return parent->beforeDelete();
	}
	protected function beforeInsert () 
	{
		$this->setRDT($DataType->getCurrentDataTime());
		if (($this->getSrcType() == SrcType::$BPClass))
		{
			$ens = $ClassFactory->GetEns($this->getNo());
			$en = $ens->getNewEntity();
			$this->setName($en->getEnDesc());
			if (($en->getIsTreeEntity() ==  TRUE ))
			{
				$this->setCodeStruct(CodeStruct::$Tree);
			}
			else
			{
				$this->setCodeStruct(CodeStruct::$NoName);
			}
		}
		if (($this->getSrcType() == SrcType::$CreateTable))
		{
			if ((DBAccess->IsExitsObject($this->getNo()) ==  TRUE ))
			{
				return parent->beforeInsert();
			}
			$sql = "";
			if ((($this->getCodeStruct() == CodeStruct::$NoName) || ($this->getCodeStruct() == CodeStruct::$GradeNoName)))
			{
				$sql = (("CREATE TABLE " . $this->getNo()) . " (");
				$sql += "No varchar(30) NOT NULL,";
				$sql += "Name varchar(3900) NULL";
				$sql += ")";
			}
			if (($this->getCodeStruct() == CodeStruct::$Tree))
			{
				$sql = (("CREATE TABLE " . $this->getNo()) . " (");
				$sql += "No varchar(30) NOT NULL,";
				$sql += "Name varchar(3900)  NULL,";
				$sql += "ParentNo varchar(3900)  NULL";
				$sql += ")";
			}
			$this->RunSQL($sql);
			$this->InitDataTable();
		}
		return parent->beforeInsert();
	}
	protected function afterInsert () 
	{
		try 
		{
			if (($this->getSrcType() == SrcType::$TableOrView))
			{
				$sql = (("CREATE VIEW " . $this->getNo()) . " (");
				$sql += "[No],";
				$sql += "[Name]";
				$sql += (( (($this->getCodeStruct() == CodeStruct::$Tree)) ? ",[ParentNo])" : ")" ));
				$sql += " AS ";
				$sql += (((((((("SELECT " . $this->getColumnValue()) . " No,") . $this->getColumnText()) . " Name") . (( (($this->getCodeStruct() == CodeStruct::$Tree)) ? ((("," . $this->getParentValue()) . " ParentNo")) : "" ))) . " FROM ") . $this->getSrcTable()) . (( ($DataType->IsNullOrEmpty($this->getSelectStatement())) ? "" : ((" WHERE " . $this->getSelectStatement())) )));
				if ((SystemConfig::getAppCenterDBType() == $DBType->MySQL))
				{
					$sql = $sql->replace("[", "`")->replace("]", "`");
				}
				else
				{
					$sql = $sql->replace("[", "")->replace("]", "");
				}
				$this->RunSQL($sql);
			}
		}
		catch (\RuntimeException $ex)
		{
			$this->DirectDelete();
			throw $ex;
		}
		parent->afterInsert();
	}
	public function GenerData_bak () 
	{
		$sql = "";
		$dt = NULL;
		if (($this->getSrcType() == SrcType::$CreateTable))
		{
			$sql = ("SELECT No,Name FROM " . $this->getSrcTable());
			$dt = $this->RunSQLReturnTable($sql);
		}
		if (($this->getSrcType() == SrcType::$TableOrView))
		{
			$sql = ("SELECT No,Name FROM " . $this->getSrcTable());
			$dt = $this->RunSQLReturnTable($sql);
		}
		if (($dt == NULL))
		{
			throw new \RuntimeException("@没有判断的数�?.");
		}
		$dt->Columns->get(0)->ColumnName = "No";
		$dt->Columns->get(1)->ColumnName = "Name";
		return $dt;
	}
	public function GenerDataOfJson () 
	{
		return $BP->Tools->Json->ToJson($this->GenerHisDataTable());
	}
	public function InitDataTable () 
	{
		$dt = $this->GenerHisDataTable();
		$sql = "";
		if (($dt->Rows->size() == 0))
		{
			if (($this->getCodeStruct() == CodeStruct::$Tree))
			{
				$sql = (((("INSERT INTO " . $this->getSrcTable()) . " (No,Name,ParentNo) VALUES('1','") . $this->getName()) . "','0') ");
				$this->RunSQL($sql);
				for ($i = 1; ($i < 4); ++$i) 
				{
					$no = $String->valueOf($i);
					$no = $StringHelper->padLeft($no, 3, '0');
					$sql = (((((("INSERT INTO " . $this->getSrcTable()) . " (No,Name,ParentNo) VALUES('") . $no) . "','Item") . $no) . "','1') ");
					$this->RunSQL($sql);
				}
			}
			if (($this->getCodeStruct() == CodeStruct::$NoName))
			{
				for ($i = 1; ($i < 4); ++$i) 
				{
					$no = $String->valueOf($i);
					$no = $StringHelper->padLeft($no, 3, '0');
					$sql = (((((("INSERT INTO " . $this->getSrcTable()) . " (No,Name) VALUES('") . $no) . "','Item") . $no) . "') ");
					$this->RunSQL($sql);
				}
			}
		}
	}
}
?>
